package com.zjdiepu.www.biz.base;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.common.consts.SystemConsts;
import com.zjdiepu.www.dao.base.SysPowerMapper;
import com.zjdiepu.www.dao.base.SysRoleApplicationMapper;
import com.zjdiepu.www.ibiz.base.ISysPowerService;
import com.zjdiepu.www.model.base.dto.SysPower;
import com.zjdiepu.www.model.base.dto.SysRoleApplication;
import com.zjdiepu.www.model.base.ext.SysPowerExt;

/**
 * 菜单表Service层操作实现<br/>
 * @author xiongxiaotun<br/>
 * @time 2017年04月05日 16时02分<br/>
 * @since 1.0<br/>
 */
@Service
public class SysPowerServiceImpl extends BaseService implements ISysPowerService {
	public static final Logger logger = LoggerFactory.getLogger(SysPowerServiceImpl.class);

	@Autowired
	private SysPowerMapper sysPowerMapper;
	@Autowired
	private SysRoleApplicationMapper sysRoleApplicationMapper;
	
	@Override
	public void saveUpdateAndDelete(SysPower sysPower, OperateType operType) {
		switch (operType) {
		case add:
			sysPower.initInsertData(sysPower.getModifier(), sysPower.getModifierName());
			sysPowerMapper.insert(sysPower);
			SysRoleApplication sysRoleApplication = new SysRoleApplication();
			sysRoleApplication.initInsertData(sysPower.getModifier(), sysPower.getModifierName());
			sysRoleApplication.setFunctionId(sysPower.getId());
			sysRoleApplication.setRoleId(SystemConsts.SUPER_ADMIN_ID);
			sysRoleApplicationMapper.insert(sysRoleApplication);
			break;
		case edit:
			sysPower.initUpdateData(sysPower.getModifier(), sysPower.getModifierName());
			// 影响的记录数
			int updateCount = sysPowerMapper.updateByPrimaryKeySelective(sysPower);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			sysPower.initDeleteData(sysPower.getModifier(), sysPower.getModifierName());
			// 影响的记录数
			int deleteCount = sysPowerMapper.updateByPrimaryKeySelective(sysPower);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<SysPower> selectByExt(SysPowerExt sysPowerExt) {
		return sysPowerMapper.selectByExt(sysPowerExt);
	}
	
	@Override
	public int countByExt(SysPowerExt sysPowerExt){
		return sysPowerMapper.countByExt(sysPowerExt);
	}
	
	@Override
	public SysPower selectById(String id) {
		return sysPowerMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			sysPowerMapper.clearEmpty(fieldNames, id);
		}
	}

	@Override
	public Set<String> selectByUserName(String userName) {
		return sysPowerMapper.selectByUserName(userName);
	}

	@Override
	public List<SysPower> selectByRoleId(String roleId) {
		return sysPowerMapper.selectByRoleId(roleId);
	}
	
	@Override
	public int countRefined(SysPower sysPower) {
		return sysPowerMapper.countRefined(sysPower);
	}

	@Override
	public SysPower selectRefined(SysPower sysPower) {
		return sysPowerMapper.selectRefined(sysPower);
	}

	@Override
	public boolean repeat(SysPower sysPower) {
		int count = countRefined(sysPower);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(sysPower.getId())) {
			return true;
		}
		SysPower dbItem = selectRefined(sysPower);
		if(null == dbItem) {
			return false;
		}
		return !sysPower.getId().equals(dbItem.getId());
	}

	@Override
	public List<SysPower> selectQuickByUserId(String userId) {
		return sysPowerMapper.selectQuickByUserId(userId);
	}
}
